/*
 * This code belongs to
 * Krishna Brahmam, Dept. of CSE, IIT Guwahati
 */

package speech;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @author Krishna Brahmam
 */
public class TrainingGUI extends javax.swing.JFrame {
    private MainGUI parent;
    
    /** Creates new form TrainingGUI */
    public TrainingGUI(MainGUI parent) {
        this.parent = parent;
        initComponents();
        
        order.setText(parent.getOrder()+"");
        frameLength.setText(parent.getFrameLength()+"");
        frameShift.setText(parent.getFrameShift()+"");
        codebookSize.setText(parent.getCodebookSize()+"");
        states.setText(parent.getStates()+"");
        
        samplesDirectoryName.setText(parent.getSamplesDirectory());
        testSamplesDirectory.setText(parent.getTestSamplesDirectory());
        
        indexFileName.setText(parent.getIndexFileName());
        listFileName.setText(parent.getListFileName());
        codebookFileName.setText(parent.getCodebookFileName());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        indexFileName = new javax.swing.JTextField();
        indexButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        listFileName = new javax.swing.JTextField();
        listButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        codebookFileName = new javax.swing.JTextField();
        codebookButton = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        samplesDirectoryName = new javax.swing.JTextField();
        samplesButton = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        testSamplesDirectory = new javax.swing.JTextField();
        testSamplesButton = new javax.swing.JButton();
        trainButton = new javax.swing.JButton();
        saveCheck = new javax.swing.JCheckBox();
        jLabel6 = new javax.swing.JLabel();
        order = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        frameLength = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        frameShift = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        codebookSize = new javax.swing.JTextField();
        cancelButton = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        states = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Training");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Training"));

        jLabel1.setText("Index file");

        indexFileName.setEditable(false);

        indexButton.setText("Browse");
        indexButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                indexButtonActionPerformed(evt);
            }
        });

        jLabel2.setText("List");

        listFileName.setEditable(false);

        listButton.setText("Browse");
        listButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                listButtonActionPerformed(evt);
            }
        });

        jLabel3.setText("Codebook");

        codebookFileName.setEditable(false);

        codebookButton.setText("Browse");
        codebookButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                codebookButtonActionPerformed(evt);
            }
        });

        jLabel4.setText("Samples");

        samplesDirectoryName.setEditable(false);

        samplesButton.setText("Browse");
        samplesButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                samplesButtonActionPerformed(evt);
            }
        });

        jLabel5.setText("Test Samples");

        testSamplesDirectory.setEditable(false);

        testSamplesButton.setText("Browse");
        testSamplesButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                testSamplesButtonActionPerformed(evt);
            }
        });

        trainButton.setText("Train");
        trainButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                trainButtonActionPerformed(evt);
            }
        });

        saveCheck.setText("new");
        saveCheck.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                saveCheckItemStateChanged(evt);
            }
        });

        jLabel6.setText("LPC Order");

        order.setEditable(false);

        jLabel7.setText("Frame Length");

        frameLength.setEditable(false);

        jLabel8.setText("Frame Shift");

        frameShift.setEditable(false);

        jLabel9.setText("Codebook Size");

        codebookSize.setEditable(false);

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        jLabel10.setText("# HMM States");

        states.setEditable(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addGap(236, 236, 236))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(32, 32, 32)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(indexFileName, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
                                    .addComponent(listFileName, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
                                    .addComponent(codebookFileName, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
                                    .addComponent(samplesDirectoryName, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
                                    .addComponent(testSamplesDirectory, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
                                    .addComponent(frameLength, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
                                    .addComponent(order, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
                                    .addComponent(frameShift, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
                                    .addComponent(codebookSize, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
                                    .addComponent(states, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(trainButton)
                                    .addComponent(saveCheck))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(listButton)
                                .addComponent(indexButton)
                                .addComponent(codebookButton)
                                .addComponent(samplesButton)
                                .addComponent(testSamplesButton))
                            .addComponent(cancelButton)))
                    .addComponent(jLabel10))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(indexFileName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(indexButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(listFileName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(listButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(codebookFileName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(saveCheck)
                    .addComponent(codebookButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(samplesDirectoryName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(samplesButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(testSamplesDirectory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(testSamplesButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(order, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(frameLength, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(frameShift, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(codebookSize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(states, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(trainButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(16, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void indexButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_indexButtonActionPerformed
    JFileChooser fc = new JFileChooser();
    if(JFileChooser.APPROVE_OPTION == fc.showOpenDialog(this)){
        indexFileName.setText(fc.getSelectedFile().getAbsolutePath());
    }
}//GEN-LAST:event_indexButtonActionPerformed

private void listButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_listButtonActionPerformed
    JFileChooser fc = new JFileChooser();
    if(JFileChooser.APPROVE_OPTION == fc.showOpenDialog(this)){
        listFileName.setText(fc.getSelectedFile().getAbsolutePath());
    }
}//GEN-LAST:event_listButtonActionPerformed

private void codebookButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_codebookButtonActionPerformed
    JFileChooser fc = new JFileChooser();
    if(saveCheck.isSelected()){
        if(JFileChooser.APPROVE_OPTION == fc.showSaveDialog(this)){
            codebookFileName.setText(fc.getSelectedFile().getAbsolutePath());
        }
    }
    else{
        if(JFileChooser.APPROVE_OPTION == fc.showOpenDialog(this)){
            codebookFileName.setText(fc.getSelectedFile().getAbsolutePath());
        }
    }
}//GEN-LAST:event_codebookButtonActionPerformed

private void saveCheckItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_saveCheckItemStateChanged
    if(saveCheck.isSelected())
        codebookFileName.setEditable(true);
    else
        codebookFileName.setEditable(false);
}//GEN-LAST:event_saveCheckItemStateChanged

private void samplesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_samplesButtonActionPerformed
    JFileChooser fc = new JFileChooser();
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    if(JFileChooser.APPROVE_OPTION == fc.showOpenDialog(this)){
        samplesDirectoryName.setText(fc.getSelectedFile().getAbsolutePath());
    }
}//GEN-LAST:event_samplesButtonActionPerformed

private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
    this.dispose();
}//GEN-LAST:event_cancelButtonActionPerformed

private void trainButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_trainButtonActionPerformed
    if( checkFields()){
        parent.setDirectoryPaths(samplesDirectoryName.getText(), testSamplesDirectory.getText());
        parent.setFileNames(indexFileName.getText(), listFileName.getText(), codebookFileName.getText());
        Main m = new Main(parent.getOrder(), parent.getCodebookSize(),
                          parent.getFrameLength(), parent.getFrameShift(),
                          parent.getStates(), null);
        m.train(codebookFileName.getText(), 
                    indexFileName.getText(), 
                    listFileName.getText(), 
                    samplesDirectoryName.getText(), 
                    testSamplesDirectory.getText());
        JOptionPane.showMessageDialog(this, "Finished training", "Status report", JOptionPane.INFORMATION_MESSAGE);
        dispose();
    }
    else{
        JOptionPane.showMessageDialog(this, "Please fill in all details", "Invalid details", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_trainButtonActionPerformed

private void testSamplesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_testSamplesButtonActionPerformed
    JFileChooser fc = new JFileChooser();
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    if(JFileChooser.APPROVE_OPTION == fc.showOpenDialog(this)){
        testSamplesDirectory.setText(fc.getSelectedFile().getAbsolutePath());
    }
}//GEN-LAST:event_testSamplesButtonActionPerformed

private boolean checkFields(){
    if(!indexFileName.getText().equals("") 
            && !listFileName.getText().equals("")
            && !codebookFileName.getText().equals("") 
            && !order.getText().equals("")
            && !frameLength.getText().equals("") 
            && !frameShift.getText().equals("")
            && !codebookSize.getText().equals("")
            && !states.getText().equals("")){
        return true;
    }
    return false;
}
  
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton codebookButton;
    private javax.swing.JTextField codebookFileName;
    private javax.swing.JTextField codebookSize;
    private javax.swing.JTextField frameLength;
    private javax.swing.JTextField frameShift;
    private javax.swing.JButton indexButton;
    private javax.swing.JTextField indexFileName;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton listButton;
    private javax.swing.JTextField listFileName;
    private javax.swing.JTextField order;
    private javax.swing.JButton samplesButton;
    private javax.swing.JTextField samplesDirectoryName;
    private javax.swing.JCheckBox saveCheck;
    private javax.swing.JTextField states;
    private javax.swing.JButton testSamplesButton;
    private javax.swing.JTextField testSamplesDirectory;
    private javax.swing.JButton trainButton;
    // End of variables declaration//GEN-END:variables

}
